package org.jadedanha.dao;

import java.sql.*;

public class ConnectionFactory {
    private static ConnectionFactory instance;
    private Connection connection;

    private ConnectionFactory() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jadedanha", "root", "password");
        conn.setAutoCommit(true);

        this.connection = conn;
    }

    public static Connection getConnection() {
        if ( ConnectionFactory.instance == null ) {
            try {
                ConnectionFactory.instance = new ConnectionFactory();
            }
            //TODO: tratar melhor o erro
            catch( Exception e ) {
                e.printStackTrace();
            }
        }

        return ConnectionFactory.instance.connection;
    }
}
